/************************************************************
 * @file   gkt_cm0_boot_gcc.S
 * @brief  cortex-m0 boot file for arm_gcc
 ************************************************************/
#include "gkt_config.h"

	.syntax	unified
	.arch	armv6-m

	.text
	.thumb
	.thumb_func
	.align	2
	.globl	Isr_Transmit
	.type	Isr_Transmit, %function
	.extern gc_sysirq_handlers
Isr_Transmit:
	ldr r1,=gc_sysirq_handlers
	ldr r0,[r1, r0]
	bx  r0
	nop
	nop
	nop

	.text
	.thumb
	.thumb_func
	.globl	Reset_Handler
	.type	Reset_Handler, %function
	.extern gc_sysirq_handlers
	.extern gkt_board_bootup
Reset_Handler:
	ldr r0, =gc_sysirq_handlers
	ldr r0, [r0,#0]
	mov	sp, r0
	ldr r0, =SystemInit
	blx	r0

	/* copy data from ROM/FLASH to SRAM */
.copy_data_init:
	ldr	r4, =__copy_table_start__
	ldr	r5, =__copy_table_end__
.copy_data:
	cmp	r4, r5
	bge	.copy_data_done
	ldr	r1, [r4]
	ldr	r2, [r4, #4]
	ldr	r3, [r4, #8]
	movs	r3, r3, lsr #4
	beq	.copy_data_4
.copy_data_16_loop:
	ldr	r0, [r1]
	str	r0, [r2]
	ldr	r0, [r1, #4]
	str	r0, [r2, #4]
	ldr	r0, [r1, #8]
	str	r0, [r2, #8]
	ldr	r0, [r1, #12]
	str	r0, [r2, #12]
	adds	r1, #16
	adds	r2, #16
	subs	r3, #1
	bne	.copy_data_16_loop
.copy_data_4:
	ldr	r3, [r4, #8]
	movs	r3, r3, lsr #2
	movs	r6, #3
	ands	r3, r3, r6
	beq	.copy_data_next
.copy_data_4_loop:
	ldr	r0, [r1]
	str	r0, [r2]
	adds	r1, #4
	adds	r2, #4
	subs	r3, #1
	bne	.copy_data_4_loop
.copy_data_next:
	adds	r4, #12
	b	.copy_data
.copy_data_done:
	
	/* clear bss/zero section(s) */
.zero_section_init:
	ldr	r3, =__zero_table_start__
	ldr	r4, =__zero_table_end__
.zero_section:
	cmp	r3, r4
	bge	.zero_section_done
	movs	r0, #0
	ldr	r1, [r3]
	ldr	r2, [r3, #4]
	movs	r2, r2, lsr #4
	beq	.zero_section_4
.zero_section_16_loop:
	str	r0, [r1]
	str	r0, [r1, #4]
	str	r0, [r1, #8]
	str	r0, [r1, #12]
	adds	r1, #16
	subs	r2, #1
	bne	.zero_section_16_loop
.zero_section_4:
	ldr	r2, [r3, #4]
	movs	r2, r2, lsr #2
	movs	r5, #3
	ands	r2, r2, r5
	beq	.zero_section_done
.zero_section_4_loop:
	str	r0, [r1]
	adds	r1, #4
	subs	r2, #1
	bne	.zero_section_4_loop
.zero_section_done:
	
	/* jump to board bootup */
	ldr	r0, =gkt_board_bootup
	bx	r0

	.pool
	.size	Reset_Handler, . - Reset_Handler

